package com.edu.dao.impl;

import com.edu.beans.Vip;
import com.edu.dao.VipsDao;
import com.edu.exception.UsedException;
import com.edu.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

public class VipsDaoImpl implements VipsDao {

    QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());

    @Override
    public int addVip(Vip vip) {
        try {
            return qr.update("insert into tb_vip values(null,?,?,?,?,?,?,?,now(),now())", vip.getName(), vip.getSex(), vip.getCard(), vip.getPhone(), vip.getV_type(), vip.getStartTime(), vip.getEndTime());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Vip> findVipsByPhone(String phone) {
        try {
            return qr.query("SELECT * FROM tb_vip WHERE phone like ?", new BeanListHandler<>(Vip.class), "%" + phone + "%");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Vip> getVipsByPhone(String phone) {
        try {
            return qr.query("SELECT * FROM tb_vip WHERE phone = ?", new BeanListHandler<>(Vip.class), phone);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Vip> getVipsByCard(String card) {
        try {
            return qr.query("SELECT * FROM tb_vip WHERE card = ?", new BeanListHandler<>(Vip.class), card);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int deleteVipById(int id) {
        try {
            return qr.update("delete from tb_vip where id = ?", id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Vip findVipByid(int id) {
        try {
            return qr.query("SELECT * FROM tb_vip WHERE id = ?", new BeanHandler<>(Vip.class), id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int updateVip(Vip vip) {
        try {
            return qr.update("update tb_vip set name = ?,sex = ?,card = ?,phone = ?,v_type = ?,startTime = ?,endTime = ? ,updateTime=now() where id = ?",
                    vip.getName(),
                    vip.getSex(),
                    vip.getCard(),
                    vip.getPhone(),
                    vip.getV_type(),
                    vip.getStartTime(),
                    vip.getEndTime(),
                    vip.getId());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Vip> getAllVips() {
        try {
            return qr.query("SELECT * FROM tb_vip", new BeanListHandler<>(Vip.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
